package com.hyyt.shopjsp.share.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.share.pojo.ShopShareRegister;
import com.hyyt.shopjsp.share.service.IShopShareRegisterService;
import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.export.ExportExcel;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/4/01 0024上午 12:20.
 */
@Controller
@RequestMapping("/back/shareRegister")
public class ShopShareRegisterController extends BaseController {
	private Logger log = Logger.getLogger(ShopShareRegisterController.class);
	private String basePath = "/back/shop/shareRegister/";
	
	@Resource
	private IShopShareRegisterService shopShareRegisterService;//会员分享注册service

	/**导出excel文件的列名称**/
	private List<String> typeNameList = new ArrayList<String>();

	/**
	 * 跳转会员分享注册
	 *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoShareRegisterPage")
	public String gotoShareRegisterPage(){
		return basePath + "shareRegisterList";
	}

	/**
	 * 会员分享注册列表
	 * @param shareCustomerName 推荐人名称
	 * @param registerCustomerName 被推荐人名称
	 * @param registerStartDate 分享开始时间
	 * @param registerEndDate 分享结束时间
	 * @param page 分页
	 * @param response 结果集
	 */
	@RequestMapping(value="listShareRegister",method=RequestMethod.POST)
	@ResponseBody
	public void listShareRegister(String shareCustomerName,String registerCustomerName,String registerStartDate,String registerEndDate,Page page, HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(shareCustomerName)){
			param.put("shareCustomerName", shareCustomerName.trim());
		}
		if(Utils.objectIsNotEmpty(registerCustomerName)){
			param.put("registerCustomerName", registerCustomerName.trim());
		}
		if(Utils.objectIsNotEmpty(registerStartDate)){
			param.put("registerStartDate", registerStartDate);
		}
		if(Utils.objectIsNotEmpty(registerEndDate)){
			param.put("registerEndDate", registerEndDate);
		}
		param.put("sortColumns", "shareTime desc");
		int totalRecordCount =shopShareRegisterService.countShopShareRegister(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShareRegister> shareRegisterList=shopShareRegisterService.queryShopShareRegister(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", shareRegisterList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 导出会员分享注册Excel
	 * @param shareCustomerName 推荐人名称
	 * @param registerCustomerName 被推荐人名称
	 * @param registerStartDate 分享开始时间
	 * @param registerEndDate 分享结束时间
	 * @param response 结果集
	 */
	@RequestMapping("exportShareRegisterExcel")
	public void exportShareRigsterExcel(String shareCustomerName,String registerCustomerName,String registerStartDate,String registerEndDate,HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(shareCustomerName)){
			//用于get请求强转，防止中文乱码
			shareCustomerName= new String(shareCustomerName.trim().getBytes("iso8859-1"),"UTF-8");
			param.put("shareCustomerName", shareCustomerName);
		}
		if(Utils.objectIsNotEmpty(registerCustomerName)){
			//用于get请求强转，防止中文乱码
			registerCustomerName= new String(registerCustomerName.trim().getBytes("iso8859-1"),"UTF-8");
			param.put("registerCustomerName", registerCustomerName);
		}
		if(Utils.objectIsNotEmpty(registerStartDate)){
			param.put("registerStartDate", registerStartDate);
		}
		if(Utils.objectIsNotEmpty(registerEndDate)){
			param.put("registerEndDate", registerEndDate);
		}
		param.put("sortColumns", "s.shareTime desc");
		List<ShopShareRegister> shareRigsterList=shopShareRegisterService.queryShopShareRegister(param);
		SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		List<Map<String,Object>> shareRigster = new ArrayList<>();
		if(shareRigsterList!=null&&shareRigsterList.size()>0){
			for (ShopShareRegister shopSharerigster : shareRigsterList) {
				 Map<String,Object> map = new HashMap<>();
				 map.put("scName", shopSharerigster.getShareCustomerName());
				 map.put("rcName", shopSharerigster.getRegisterCustomerName());
				 map.put("giveCoinNumber", shopSharerigster.getGiveCoinNumber());
				 map.put("shareTime", formatter.format(shopSharerigster.getShareTime()));
				 shareRigster.add(map);
			}
		}
			String fileName ="会员分享注册明细";
			String columnNames[] = {"推荐人名称","被推荐人名称","赠送金币数","分享时间"};
			String keys[] = {"scName","rcName","giveCoinNumber","shareTime"};
			try {
	            ExportExcel.download(fileName,shareRigster,columnNames,keys,request,response);
	        } catch (IOException e) {
	            logger.error("导出Excel失败！");
	        }
		
	}

}